package com.yscp.ysdemo.mq;

import com.yscp.ysdemo.constant.Constants;
import com.yscp.ysdemo.constant.MqConstants;
import com.yscp.ysdemo.dto.LikeDto;
import com.yscp.ysdemo.entity.Notify;
import com.yscp.ysdemo.mapper.NotifyMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 监听三连保存请求的消费者，负责将内容存入数据库
 */
@Component
public class MqWorkListener {

    @Resource
    private NotifyMapper notifyMapper;


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("CollectQueue"),
                    exchange = @Exchange(value = MqConstants.EXCHANGE_OPTION_KEY,type =  ExchangeTypes.DIRECT),
                    key = {MqConstants.OPTION_COLLECT}
            )
    })
    public void consumeOptionCollect(Object msg){

    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("likeQueue"),
                    exchange = @Exchange(value = MqConstants.EXCHANGE_OPTION_KEY,type =  ExchangeTypes.DIRECT),
                    key = {MqConstants.OPTION_LIKE}
            )
    })
    /**
     * 往消息表插入点赞消息
     */
    public void consumeOptionLike(LikeDto msg){
        System.out.println(msg);
        Notify notify = new Notify();
        notify.setAction(Constants.ACTION_LIKE);
        notify.setTargetId(msg.getEntityId());
        notify.setTargetType(msg.getEntityType());
        notify.setSender(msg.getUserId());
        notify.setType(Constants.NOTICE_REMIND);
        notifyMapper.insert(notify);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("focusQueue"),
                    exchange = @Exchange(value = MqConstants.EXCHANGE_OPTION_KEY,type =  ExchangeTypes.DIRECT),
                    key = {MqConstants.OPTION_FOCUS}
            )
    })
    public void consumeOptionFocus(Object msg){

    }
}
